package com.urban.pooling.dto;

import com.urban.pooling.domain.User;

public class UserDto {
	private int userId;
	private String email;
	private String firstName;
	private String lastName;
	private String username;
	private String pass;

	public UserDto() {

	}

	public UserDto(User user) {
		userId = user.getUserId();
		email = user.getEmail();
		firstName = user.getFirstName();
		lastName = user.getLastName();
		username = user.getUsername();
	}

	public UserDto(int userId, String email, String firstName, String lastName,
			String username) {
		super();
		this.userId = userId;
		this.email = email;
		this.firstName = firstName;
		this.lastName = lastName;
		this.username = username;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPass() {
		return pass;
	}
	
	public void setPass(String pass) {
		this.pass = pass;
	}

	public User dtoToUser(UserDto userdto){
		User user = new User();
		
		user.setFirstName( userdto.getFirstName() != null || userdto.getFirstName() != "" ? userdto.getFirstName() : "" ); 
		user.setLastName( userdto.getLastName() != null || userdto.getLastName() != "" ? userdto.getLastName() : "" );
		user.setUsername( userdto.getUsername() != null || userdto.getUsername() != "" ? userdto.getUsername() : "");
		user.setEmail( userdto.getEmail() != null || userdto.getEmail() != "" ? userdto.getEmail() : "");
		user.setPasswordHash( userdto.getPass() != null || userdto.getPass() != "" ? userdto.getPass() : "");
		user.setEnabled(true);
		//TODO: salt
		user.setSalt("1234");
		
		return user;
	}
	
	public User toUserAndValidate() throws RuntimeException{
		User user = dtoToUser(this);
		
		if (user.getEmail().length() > 100) {
			throw new RuntimeException( "Email's length cannot be longer then 100 charachters");
		}else if (user.getUsername().length() > 45) {
			throw new RuntimeException ( "Username's length cannot be longer then 45 charachters");
		}else if (user.getFirstName().length() > 255) {
			throw new RuntimeException ( "First name's length cannot be longer then 100 charachters");
		}else if (user.getLastName().length() > 255) {
			throw new RuntimeException( "Last name's length cannot be longer then 100 charachters");
		}else if (user.getPasswordHash().length() > 45) {
			throw new RuntimeException ( "Password's length cannot be longer then 100 charachters");
		}
		
		return user;
	}

}
